﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.SysPage
{
    [Function("SysPage.PageDetail")]
    public partial class PageDetail : PageBase
    {
        private string CurrentActionID
        {
            get
            {
                return GetStringByParameter("ActionID");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
            }
        }

        private void LoadDefault()
        {

            if (!string.IsNullOrEmpty(CurrentActionID))
            {
                var pageEntity = new ActionEntity(CurrentActionID);
                if (!pageEntity.IsNew)
                {

                    txtActionID.Text = pageEntity.ActionId;
                    txtActionName.Text = pageEntity.ActionName;
                    txtParentID.Text = pageEntity.ParentId;

                   txtDBTableName.Text= pageEntity.DbtableName;
                    txtNavigatorUrl.Text = pageEntity.NavigateUrl;
                    chkActive.Checked = pageEntity.IsActive;
                    txtImageUrl.Text = pageEntity.ImageUrl;
                    txtSortIndex.Text = pageEntity.SortIndex;
                    lblCreateDate.Text = pageEntity.CreateDate.ToString(ApplicationConfiguration.DateTimeFormat);
                    txtDescription.Text = pageEntity.Description;
                    chkShowEventButton.Checked = pageEntity.IsShowEventButton;
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var pageEntity = new ActionEntity(CurrentActionID);
            if (pageEntity.IsNew)
            {
                //check action id is unique
                var collection = new ActionCollection();
                var intCount = collection.GetDbCount(ActionFields.ActionId == txtActionID.Text.Trim());
                if(intCount>0)
                {
                    Alert("The Page ID is already exist in the database,please use another.");
                    return;
                }
            }else
            {
                //check action id is unique
                var collection = new ActionCollection();
                var intCount = collection.GetDbCount(ActionFields.ActionId ==CurrentActionID);
                if (intCount >1)
                {
                    Alert("The Page ID is already exist in the database,please use another.");
                    return;
                }
            }
            pageEntity.ActionId = txtActionID.Text.Trim();
            pageEntity.ActionName = txtActionName.Text.Trim();
            pageEntity.ParentId = txtParentID.Text.Trim();
            pageEntity.DbtableName = txtDBTableName.Text.Trim();
            pageEntity.NavigateUrl = txtNavigatorUrl.Text.Trim();
            pageEntity.IsActive = chkActive.Checked;
            pageEntity.ImageUrl = txtImageUrl.Text.Trim();
            pageEntity.SortIndex = txtSortIndex.Text.Trim();
            pageEntity.Description = txtDescription.Text.Trim();
            pageEntity.IsShowEventButton = chkShowEventButton.Checked;
            if (pageEntity.Save())
            {
                RediretResolveUrl(SiteUrls.SysPage.PageList);
                return;
            }
            else
            {
                Alert(GenerateAlert.SaveFailed);
            }

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            RediretResolveUrl(SiteUrls.SysPage.PageList);
        }

    }
}
